<template>
  <div class="user-profile">
    <!-- 用户信息头部 -->
    <header class="profile-header">
      <div class="profile-banner">
        <img
          v-if="userInfo.banner"
          :src="userInfo.banner"
          alt="背景图"
          class="banner-image"
        />
        <div v-else class="banner-placeholder"></div>
        <div class="profile-actions">
          <button v-if="!isOwnProfile" class="follow-btn">
            {{ isFollowing ? '已关注' : '关注' }}
          </button>
          <button v-else class="edit-btn" @click="handleEdit">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            编辑资料
          </button>
        </div>
      </div>
      <div class="profile-info">
        <div class="avatar-container">
          <div v-if="!userInfo.avatar" class="avatar-large">
            {{ userInfo.name?.charAt(0) || 'U' }}
          </div>
          <img v-else :src="userInfo.avatar" :alt="userInfo.name" class="avatar-large-img" />
        </div>
        <div class="user-details">
          <h1 class="user-name">{{ userInfo.name || '未设置昵称' }}</h1>
          <p class="user-bio">{{ userInfo.bio || '这个人很懒，什么都没有留下...' }}</p>
          <div class="user-stats">
            <div class="stat-item" @click="showArticles">
              <span class="stat-value">{{ userInfo.articleCount || 0 }}</span>
              <span class="stat-label">文章</span>
            </div>
            <div class="stat-item" @click="showFollowers">
              <span class="stat-value">{{ userInfo.followerCount || 0 }}</span>
              <span class="stat-label">粉丝</span>
            </div>
            <div class="stat-item" @click="showFollowing">
              <span class="stat-value">{{ userInfo.followingCount || 0 }}</span>
              <span class="stat-label">关注</span>
            </div>
            <div class="stat-item" @click="showCollections">
              <span class="stat-value">{{ userInfo.collectionCount || 0 }}</span>
              <span class="stat-label">收藏</span>
            </div>
          </div>
          <div class="user-tags" v-if="userInfo.tags && userInfo.tags.length">
            <span v-for="tag in userInfo.tags" :key="tag" class="user-tag">{{ tag }}</span>
          </div>
        </div>
      </div>
    </header>

    <!-- 内容标签页 -->
    <div class="content-tabs">
      <div class="tabs-container">
        <button
          v-for="tab in tabs"
          :key="tab.key"
          :class="['tab-button', { active: activeTab === tab.key }]"
          @click="activeTab = tab.key"
        >
          {{ tab.label }}
        </button>
      </div>
    </div>

    <!-- 内容区域 -->
    <main class="profile-content">
      <div class="content-container">
        <!-- 文章列表 -->
        <div v-if="activeTab === 'articles'" class="content-section">
          <div v-if="userArticles.length === 0" class="empty-content">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <polyline points="14 2 14 8 20 8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <p>还没有发布文章</p>
          </div>
          <div v-else class="articles-grid">
            <ArticleCard
              v-for="article in userArticles"
              :key="article.id"
              :article="article"
              @click="handleArticleClick"
            />
          </div>
        </div>

        <!-- 收藏列表 -->
        <div v-if="activeTab === 'collections'" class="content-section">
          <div v-if="collectedArticles.length === 0" class="empty-content">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <p>还没有收藏文章</p>
          </div>
          <div v-else class="articles-grid">
            <ArticleCard
              v-for="article in collectedArticles"
              :key="article.id"
              :article="article"
              @click="handleArticleClick"
            />
          </div>
        </div>

        <!-- 旅行日志 -->
        <div v-if="activeTab === 'logs'" class="content-section">
          <div v-if="travelLogs.length === 0" class="empty-content">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" stroke="currentColor" stroke-width="2"/>
              <circle cx="12" cy="10" r="3" stroke="currentColor" stroke-width="2"/>
            </svg>
            <p>还没有旅行日志</p>
          </div>
          <div v-else class="logs-list">
            <div v-for="log in travelLogs" :key="log.id" class="log-item">
              <div class="log-date">{{ formatDate(log.date) }}</div>
              <div class="log-content">
                <h3 class="log-title">{{ log.title }}</h3>
                <p class="log-description">{{ log.description }}</p>
                <div class="log-images" v-if="log.images && log.images.length">
                  <img
                    v-for="(img, idx) in log.images.slice(0, 3)"
                    :key="idx"
                    :src="img"
                    alt="旅行照片"
                    class="log-image"
                  />
                </div>
                <div class="log-meta">
                  <span class="log-location">{{ log.location }}</span>
                  <span class="log-likes">
                    <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                      <path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                    {{ log.likeCount || 0 }}
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import ArticleCard from '@/components/article/ArticleCard.vue'

const route = useRoute()

// 响应式数据
const activeTab = ref('articles')
const isFollowing = ref(false)
const isOwnProfile = ref(true) // TODO: 从路由参数判断

const tabs = [
  { key: 'articles', label: '文章' },
  { key: 'collections', label: '收藏' },
  { key: 'logs', label: '旅行日志' }
]

// 用户信息（模拟数据）
const userInfo = ref({
  id: 1,
  name: '旅行达人小王',
  bio: '热爱旅行，喜欢分享旅行中的美好瞬间。足迹遍布30多个国家，记录每一次旅行的精彩时刻。',
  avatar: null,
  banner: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=1200',
  articleCount: 12,
  followerCount: 128,
  followingCount: 45,
  collectionCount: 38,
  tags: ['城市游', '美食游', '摄影游']
})

// 用户文章（模拟数据）
const userArticles = ref([
  {
    id: 1,
    title: '三天两夜杭州深度游，西湖美景尽收眼底',
    excerpt: '杭州，一座充满诗意的城市。三天两夜的行程，带你领略西湖的四季美景...',
    cover: 'https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?w=800',
    authorName: '旅行达人小王',
    authorAvatar: null,
    tags: ['城市游', '美食游'],
    likeCount: 128,
    commentCount: 45,
    publishTime: new Date(Date.now() - 2 * 3600000).toISOString()
  },
  {
    id: 2,
    title: '成都美食地图：不可错过的10家必吃餐厅',
    excerpt: '成都，美食之都。从火锅到串串，从麻辣烫到担担面...',
    cover: 'https://images.unsplash.com/photo-1555396273-367ea4eb4db5?w=800',
    authorName: '旅行达人小王',
    authorAvatar: null,
    tags: ['美食游', '城市游'],
    likeCount: 342,
    commentCount: 156,
    publishTime: new Date(Date.now() - 1 * 86400000).toISOString()
  }
])

// 收藏的文章（模拟数据）
const collectedArticles = ref([
  {
    id: 3,
    title: '自驾川藏线：318国道的壮美之旅',
    excerpt: '从成都出发，沿着318国道一路向西，感受青藏高原的壮美风光...',
    cover: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800',
    authorName: '越野爱好者',
    authorAvatar: null,
    tags: ['自驾游', '背包客'],
    likeCount: 256,
    commentCount: 89,
    publishTime: new Date(Date.now() - 5 * 3600000).toISOString()
  }
])

// 旅行日志（模拟数据）
const travelLogs = ref([
  {
    id: 1,
    title: '西湖边的清晨',
    description: '清晨的西湖，薄雾缭绕，如诗如画。漫步在断桥上，感受着这座城市的温柔...',
    date: new Date(Date.now() - 3 * 86400000).toISOString(),
    location: '杭州·西湖',
    images: [
      'https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?w=400',
      'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400'
    ],
    likeCount: 45
  },
  {
    id: 2,
    title: '成都火锅之旅',
    description: '终于吃到了正宗的成都火锅，麻辣鲜香，让人回味无穷...',
    date: new Date(Date.now() - 7 * 86400000).toISOString(),
    location: '成都·宽窄巷子',
    images: [
      'https://images.unsplash.com/photo-1555396273-367ea4eb4db5?w=400'
    ],
    likeCount: 32
  }
])

// 方法
function handleEdit() {
  // TODO: 跳转到编辑资料页面
  console.log('编辑资料')
}

function handleArticleClick(article) {
  // TODO: 跳转到文章详情页
  console.log('查看文章:', article.id)
}

function showArticles() {
  activeTab.value = 'articles'
}

function showFollowers() {
  // TODO: 显示粉丝列表
  console.log('显示粉丝')
}

function showFollowing() {
  // TODO: 显示关注列表
  console.log('显示关注')
}

function showCollections() {
  activeTab.value = 'collections'
}

function formatDate(date) {
  if (!date) return ''
  const d = new Date(date)
  return `${d.getMonth() + 1}月${d.getDate()}日`
}

onMounted(() => {
  // TODO: 从路由参数获取用户ID，加载用户信息
  const userId = route.params.userId
  if (userId) {
    // 加载用户数据
  }
})
</script>

<style scoped>
.user-profile {
  min-height: calc(100vh - 64px);
  background: #f9fafb;
  padding-top: 64px;
}

.profile-header {
  background: #ffffff;
  border-bottom: 1px solid #eef0f5;
}

.profile-banner {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.banner-placeholder {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.profile-actions {
  position: absolute;
  top: 16px;
  right: 16px;
}

.follow-btn,
.edit-btn {
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #2b2f36;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.2s ease;
}

.follow-btn:hover,
.edit-btn:hover {
  background: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.edit-btn svg {
  width: 16px;
  height: 16px;
}

.profile-info {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px 24px;
  display: flex;
  gap: 24px;
  align-items: flex-start;
  margin-top: -60px;
}

.avatar-container {
  position: relative;
}

.avatar-large {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  font-weight: 700;
  border: 4px solid #ffffff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.avatar-large-img {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 4px solid #ffffff;
  object-fit: cover;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.user-details {
  flex: 1;
  padding-top: 60px;
}

.user-name {
  font-size: 28px;
  font-weight: 700;
  color: #2b2f36;
  margin: 0 0 8px 0;
}

.user-bio {
  font-size: 15px;
  color: #6b7280;
  line-height: 1.6;
  margin: 0 0 16px 0;
}

.user-stats {
  display: flex;
  gap: 32px;
  margin-bottom: 16px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  cursor: pointer;
  transition: color 0.2s ease;
}

.stat-item:hover {
  color: #667eea;
}

.stat-value {
  font-size: 20px;
  font-weight: 700;
  color: #2b2f36;
}

.stat-label {
  font-size: 14px;
  color: #9ca3af;
}

.user-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.user-tag {
  padding: 6px 12px;
  background: #f3f4f6;
  color: #667eea;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
}

.content-tabs {
  background: #ffffff;
  border-bottom: 1px solid #eef0f5;
  position: sticky;
  top: 64px;
  z-index: 10;
}

.tabs-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  gap: 8px;
}

.tab-button {
  padding: 12px 24px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  font-size: 15px;
  color: #6b7280;
  cursor: pointer;
  transition: all 0.2s ease;
}

.tab-button:hover {
  color: #667eea;
}

.tab-button.active {
  color: #667eea;
  border-bottom-color: #667eea;
  font-weight: 600;
}

.profile-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

.content-section {
  min-height: 400px;
}

.articles-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
}

.empty-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  color: #9ca3af;
}

.empty-content svg {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
  color: #d1d5db;
}

.empty-content p {
  font-size: 16px;
  color: #6b7280;
}

.logs-list {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.log-item {
  background: #ffffff;
  border-radius: 12px;
  padding: 20px;
  border: 1px solid #eef0f5;
  display: flex;
  gap: 20px;
}

.log-date {
  min-width: 80px;
  font-size: 14px;
  color: #9ca3af;
  font-weight: 500;
}

.log-content {
  flex: 1;
}

.log-title {
  font-size: 18px;
  font-weight: 600;
  color: #2b2f36;
  margin: 0 0 8px 0;
}

.log-description {
  font-size: 14px;
  color: #6b7280;
  line-height: 1.6;
  margin: 0 0 12px 0;
}

.log-images {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.log-image {
  width: 120px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
}

.log-meta {
  display: flex;
  gap: 16px;
  align-items: center;
  font-size: 13px;
  color: #9ca3af;
}

.log-location {
  display: flex;
  align-items: center;
}

.log-likes {
  display: flex;
  align-items: center;
  gap: 4px;
}

.log-likes svg {
  width: 16px;
  height: 16px;
}
</style>

